// 奥数知识点统计分析
// 小学和初中知识点的详细分布

import { EXTENDED_TOPICS } from './extended-topics';
import { MIDDLE_SCHOOL_TOPICS } from './middle-school-topics';

// 统计分析函数
export function analyzeTopics() {
  // 分析原有的36个知识点
  const primaryTopics = EXTENDED_TOPICS.filter(topic => 
    topic.gradeLevel.every(grade => grade <= 5)
  );
  
  const mixedTopics = EXTENDED_TOPICS.filter(topic => 
    topic.gradeLevel.some(grade => grade <= 5) && 
    topic.gradeLevel.some(grade => grade >= 6)
  );
  
  const middleTopics = EXTENDED_TOPICS.filter(topic => 
    topic.gradeLevel.every(grade => grade >= 6)
  );

  return {
    total: EXTENDED_TOPICS.length,
    primaryOnly: primaryTopics.length,
    mixed: mixedTopics.length,
    middleOnly: middleTopics.length,
    newMiddleSchool: MIDDLE_SCHOOL_TOPICS.length,
    grandTotal: EXTENDED_TOPICS.length + MIDDLE_SCHOOL_TOPICS.length
  };
}

// 按年级分布统计
export function getGradeDistribution() {
  const distribution: { [key: number]: string[] } = {};
  
  // 统计原有知识点
  EXTENDED_TOPICS.forEach(topic => {
    topic.gradeLevel.forEach(grade => {
      if (!distribution[grade]) {
        distribution[grade] = [];
      }
      distribution[grade].push(topic.name);
    });
  });
  
  // 统计新增初中知识点
  MIDDLE_SCHOOL_TOPICS.forEach(topic => {
    topic.gradeLevel.forEach(grade => {
      if (!distribution[grade]) {
        distribution[grade] = [];
      }
      distribution[grade].push(`${topic.name}(初中)`);
    });
  });
  
  return distribution;
}

// 按分类统计
export function getCategoryDistribution() {
  const primaryCategories: { [key: string]: number } = {};
  const middleCategories: { [key: string]: number } = {};
  
  // 统计小学知识点分类
  EXTENDED_TOPICS.forEach(topic => {
    if (topic.gradeLevel.some(grade => grade <= 5)) {
      primaryCategories[topic.category] = (primaryCategories[topic.category] || 0) + 1;
    }
  });
  
  // 统计初中知识点分类
  MIDDLE_SCHOOL_TOPICS.forEach(topic => {
    middleCategories[topic.category] = (middleCategories[topic.category] || 0) + 1;
  });
  
  return { primaryCategories, middleCategories };
}

// 详细的知识点分布报告
export const TOPICS_REPORT = {
  // 小学奥数知识点（1-5年级）
  primarySchool: {
    total: 0, // 将通过函数计算
    categories: {
      '基础计算': [
        '鸡兔同笼', '和差倍问题', '植树问题', '盈亏问题'
      ],
      '行程问题': [
        '相遇问题', '追及问题', '环形跑道'
      ],
      '几何图形': [
        '周长面积', '立体图形', '图形计数'
      ],
      '数论基础': [
        '质数合数', '因数倍数', '奇偶性质'
      ],
      '逻辑推理': [
        '找规律', '逻辑推理', '抽屉原理', '容斥原理'
      ],
      '应用问题': [
        '工程问题', '时钟问题'
      ],
      '组合计数': [
        '数字谜题', '幻方问题'
      ]
    }
  },
  
  // 初中奥数知识点（6-9年级）
  middleSchool: {
    total: MIDDLE_SCHOOL_TOPICS.length,
    categories: {
      '代数基础': [
        '代数式运算', '一元一次方程', '一元一次不等式', 
        '二元一次方程组', '一元二次方程', '分式方程'
      ],
      '函数': [
        '函数基础', '一次函数', '反比例函数', '二次函数'
      ],
      '几何基础': [
        '几何证明', '三角形性质', '勾股定理', 
        '四边形性质', '圆的性质', '相似与全等'
      ],
      '数论进阶': [
        '整除性质', '同余运算', '不定方程'
      ],
      '组合数学': [
        '高级计数', '图论初步'
      ],
      '概率统计': [
        '概率进阶', '统计分析'
      ]
    }
  },
  
  // 跨年级知识点（小学高年级到初中）
  crossGrade: {
    topics: [
      '年龄问题(4-6年级)', '平均数问题(4-6年级)', 
      '流水行船(6-7年级)', '浓度问题(6-7年级)', 
      '利润问题(6-7年级)', '排列组合(6-7年级)', 
      '概率统计(6-7年级)', '最值问题(6-8年级)'
    ]
  }
};

// 难度分布统计
export function getDifficultyDistribution() {
  const primaryDifficulty: { [key: number]: number } = {};
  const middleDifficulty: { [key: number]: number } = {};
  
  EXTENDED_TOPICS.forEach(topic => {
    if (topic.gradeLevel.some(grade => grade <= 5)) {
      primaryDifficulty[topic.difficulty] = (primaryDifficulty[topic.difficulty] || 0) + 1;
    }
  });
  
  MIDDLE_SCHOOL_TOPICS.forEach(topic => {
    middleDifficulty[topic.difficulty] = (middleDifficulty[topic.difficulty] || 0) + 1;
  });
  
  return { primaryDifficulty, middleDifficulty };
}

// 生成完整的统计报告
export function generateFullReport() {
  const analysis = analyzeTopics();
  const gradeDistribution = getGradeDistribution();
  const categoryDistribution = getCategoryDistribution();
  const difficultyDistribution = getDifficultyDistribution();
  
  return {
    summary: {
      totalTopics: analysis.grandTotal,
      primarySchoolTopics: analysis.primaryOnly + analysis.mixed,
      middleSchoolTopics: analysis.middleOnly + analysis.newMiddleSchool,
      newlyAdded: analysis.newMiddleSchool
    },
    gradeDistribution,
    categoryDistribution,
    difficultyDistribution,
    recommendations: [
      '小学阶段重点培养基础计算和逻辑思维能力',
      '初中阶段注重代数和几何的系统学习',
      '跨年级知识点需要循序渐进的教学设计',
      '高难度题目适合有一定基础的学生挑战'
    ]
  };
}

// 导出统计数据
export const STATISTICS = {
  originalTopics: EXTENDED_TOPICS.length,
  newMiddleSchoolTopics: MIDDLE_SCHOOL_TOPICS.length,
  totalTopics: EXTENDED_TOPICS.length + MIDDLE_SCHOOL_TOPICS.length,
  primarySchoolFocus: '基础计算、逻辑推理、几何入门',
  middleSchoolFocus: '代数运算、函数概念、几何证明',
  difficultyRange: '1-5级，适合不同水平学生'
};
